package com.ruyuan.eshop.construction.builder;

import com.alibaba.fastjson.JSON;
import com.ruyuan.eshop.construction.domain.entity.AttributeExtendDO;
import com.ruyuan.eshop.construction.domain.entity.CspuSkuRelationDO;
import com.ruyuan.eshop.construction.domain.entity.ItemInfoDO;
import com.ruyuan.eshop.construction.domain.entity.ItemShelfLifeDO;
import com.ruyuan.eshop.construction.domain.entity.ItemVideoImgDO;
import com.ruyuan.eshop.construction.domain.entity.QualityControlDO;
import com.ruyuan.eshop.construction.domain.entity.SkuBarCodeRelationDO;
import com.ruyuan.eshop.construction.domain.entity.SkuInfoDO;
import com.ruyuan.eshop.construction.domain.request.ProductRequest;
import com.ruyuan.eshop.common.enums.DelFlagEnum;
import com.ruyuan.eshop.common.enums.ProductTypeEnum;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 全量商品数据
 *
 * @author zhonghuashishan
 */
public class ProductBuilder {

    /**
     * 商品入参
     */
    private ProductRequest productRequest;

    /**
     * 全量商品数据
     */
    private FullProductData fullProductData;

    public ProductBuilder(ProductRequest productRequest) {
        this.productRequest = productRequest;
        this.fullProductData = new FullProductData();
    }

    public ProductBuilder buildItemInfo() {
        ItemInfoDO itemInfoDO = new ItemInfoDO();
        ProductRequest.ItemBaseRequest itemBaseRequest = productRequest.getItemBaseRequest();
        itemInfoDO.setItemId(itemBaseRequest.getItemId());
        itemInfoDO.setItemName(itemBaseRequest.getItemName());
        itemInfoDO.setItemType(itemBaseRequest.getItemType());
        itemInfoDO.setChannel(itemBaseRequest.getChannel());
        itemInfoDO.setSellerType(itemBaseRequest.getSellerType());
        itemInfoDO.setProducingAreaId(itemBaseRequest.getProducingAreaId());
        itemInfoDO.setItemStatus(itemBaseRequest.getItemStatus());
        itemInfoDO.setBrandId(itemBaseRequest.getBrandId());
        itemInfoDO.setCategoryId(itemBaseRequest.getLastCategoryId());
        itemInfoDO.setFirstCategoryId(itemBaseRequest.getOneCategoryId());
        itemInfoDO.setSecondCategoryId(itemBaseRequest.getTwoCategoryId());
        itemInfoDO.setThirdCategoryId(itemBaseRequest.getThreeCategoryId());
        ProductRequest.ItemPopularizeRequest itemPopularizeRequest = productRequest.getItemPopularizeRequest();
        itemInfoDO.setRecommend(itemPopularizeRequest.getRecommend());
        ProductRequest.ShelfLifeRequest shelfLifeRequest = productRequest.getShelfLifeRequest();
        itemInfoDO.setShelfLife(shelfLifeRequest.getShelfLife());
        ProductRequest.ItemStorageRequest itemStorageRequest = productRequest.getItemStorageRequest();
        itemInfoDO.setStoreConditionType(itemStorageRequest.getStoreConditionType());
        itemInfoDO.setItemSpecsValue(JSON.toJSONString(itemStorageRequest.getProductSpcesValueList()));
        itemInfoDO.setDelFlag(DelFlagEnum.EFFECTIVE.getCode());

        itemInfoDO.setCreateTime(new Date());
        itemInfoDO.setCreateUser(productRequest.getOperatorUser());
        itemInfoDO.setUpdateTime(new Date());
        itemInfoDO.setUpdateUser(productRequest.getOperatorUser());
        fullProductData.setItemInfoDO(itemInfoDO);
        return this;
    }

    public ProductBuilder buildItemShelfLife() {
        ItemShelfLifeDO itemShelfLifeDO = new ItemShelfLifeDO();
        ProductRequest.ItemBaseRequest itemBaseRequest = productRequest.getItemBaseRequest();
        ProductRequest.ShelfLifeRequest shelfLifeRequest = productRequest.getShelfLifeRequest();
        itemShelfLifeDO.setItemId(itemBaseRequest.getItemId());
        itemShelfLifeDO.setShelfLifeContent(JSON.toJSONString(shelfLifeRequest.getShelfLife()));
        itemShelfLifeDO.setDelFlag(DelFlagEnum.EFFECTIVE.getCode());

        itemShelfLifeDO.setCreateTime(new Date());
        itemShelfLifeDO.setCreateUser(productRequest.getOperatorUser());
        itemShelfLifeDO.setUpdateTime(new Date());
        itemShelfLifeDO.setUpdateUser(productRequest.getOperatorUser());

        fullProductData.setItemShelfLifeDO(itemShelfLifeDO);
        return this;
    }

    public ProductBuilder buildItemVideoImgList() {
        List<ItemVideoImgDO> itemVideoImgDOList = new ArrayList<>(16);
        ProductRequest.ItemBaseRequest itemBaseRequest = productRequest.getItemBaseRequest();
        List<ProductRequest.ItemVideoImgRequest> itemVideoImgRequestList = productRequest.getItemVideoImgRequestList();
        for (ProductRequest.ItemVideoImgRequest itemVideoImgRequest : itemVideoImgRequestList) {
            ItemVideoImgDO itemVideoImgDO = new ItemVideoImgDO();
            itemVideoImgDO.setItemId(itemBaseRequest.getItemId());
            itemVideoImgDO.setChannel(itemBaseRequest.getChannel());
            itemVideoImgDO.setContentType(itemVideoImgRequest.getContentType());
            itemVideoImgDO.setContentUrl(itemVideoImgRequest.getContentUrl());
            itemVideoImgDO.setContentSort(itemVideoImgRequest.getContentSort());
            itemVideoImgDO.setVersionId(Objects.isNull(productRequest.getItemBaseRequest().getItemId())? 0:null);
            itemVideoImgDO.setDelFlag(DelFlagEnum.EFFECTIVE.getCode());

            itemVideoImgDO.setCreateTime(new Date());
            itemVideoImgDO.setCreateUser(productRequest.getOperatorUser());
            itemVideoImgDO.setUpdateTime(new Date());
            itemVideoImgDO.setUpdateUser(productRequest.getOperatorUser());

            itemVideoImgDOList.add(itemVideoImgDO);
        }
        fullProductData.setItemVideoImgDOList(itemVideoImgDOList);
        return this;
    }

    public ProductBuilder buildSkuInfoList() {
        List<SkuInfoDO> skuInfoDOList = new ArrayList<>(16);
        ProductRequest.ItemBaseRequest itemBaseRequest = productRequest.getItemBaseRequest();
        List<ProductRequest.SkuInfoRequest> skuInfoRequestList = productRequest.getItemSaleRequest().getSkuInfoRequestList();
        for (ProductRequest.SkuInfoRequest skuInfoRequest : skuInfoRequestList) {
            SkuInfoDO skuInfoDO = new SkuInfoDO();
            skuInfoDO.setItemId(skuInfoRequest.getItemId());
            skuInfoDO.setSkuId(skuInfoRequest.getSkuId());
            skuInfoDO.setSkuName(skuInfoRequest.getSkuName());
            skuInfoDO.setSkuType(itemBaseRequest.getItemType());
            skuInfoDO.setBasePrice(skuInfoRequest.getBasePrice());
            skuInfoDO.setVipPrice(skuInfoRequest.getVipPrice());
            skuInfoDO.setSkuGrade(skuInfoRequest.getSkuGrade());
            skuInfoDO.setChannel(itemBaseRequest.getChannel());
            skuInfoDO.setSellerType(itemBaseRequest.getSellerType());
            skuInfoDO.setSkuSpecsValue(JSON.toJSONString(skuInfoRequest.getProductSpcesValueList()));
            skuInfoDO.setVersionId(Objects.isNull(productRequest.getItemBaseRequest().getItemId())? 0:null);
            skuInfoDO.setDelFlag(DelFlagEnum.EFFECTIVE.getCode());

            skuInfoDO.setCreateTime(new Date());
            skuInfoDO.setCreateUser(productRequest.getOperatorUser());
            skuInfoDO.setUpdateTime(new Date());
            skuInfoDO.setUpdateUser(productRequest.getOperatorUser());

            skuInfoDOList.add(skuInfoDO);
        }
        fullProductData.setSkuInfoDOList(skuInfoDOList);
        return this;
    }

    public ProductBuilder buildSkuBarCodeRelationList() {
        List<SkuBarCodeRelationDO> skuBarCodeRelationDOList = new ArrayList<>(16);
        List<ProductRequest.SkuInfoRequest> skuInfoRequestList = productRequest.getItemSaleRequest().getSkuInfoRequestList();
        for (ProductRequest.SkuInfoRequest skuInfoRequest : skuInfoRequestList) {
            SkuBarCodeRelationDO skuBarCodeRelationDO = new SkuBarCodeRelationDO();
            skuBarCodeRelationDO.setSkuId(skuInfoRequest.getSkuId());
            skuBarCodeRelationDO.setBarCode(skuInfoRequest.getBarCode());
            skuBarCodeRelationDO.setDelFlag(DelFlagEnum.EFFECTIVE.getCode());

            skuBarCodeRelationDO.setCreateTime(new Date());
            skuBarCodeRelationDO.setCreateUser(productRequest.getOperatorUser());
            skuBarCodeRelationDO.setUpdateTime(new Date());
            skuBarCodeRelationDO.setUpdateUser(productRequest.getOperatorUser());

            skuBarCodeRelationDOList.add(skuBarCodeRelationDO);
        }
        fullProductData.setSkuBarCodeRelationDOList(skuBarCodeRelationDOList);
        return this;
    }

    public ProductBuilder buildCspuSkuRelation() {
        List<CspuSkuRelationDO> cspuSkuRelationDOList = new ArrayList<>(16);
        ProductRequest.ItemBaseRequest itemBaseRequest = productRequest.getItemBaseRequest();
        List<ProductRequest.SkuInfoRequest> skuInfoRequestList = productRequest.getItemSaleRequest().getSkuInfoRequestList();
        for (ProductRequest.SkuInfoRequest skuInfoRequest : skuInfoRequestList) {
            CspuSkuRelationDO cspuSkuRelationDO = new CspuSkuRelationDO();
            cspuSkuRelationDO.setSkuId(skuInfoRequest.getSkuId());
            cspuSkuRelationDO.setCspuId(skuInfoRequest.getCspuId());
            cspuSkuRelationDO.setChannel(itemBaseRequest.getChannel());

            cspuSkuRelationDO.setCreateTime(new Date());
            cspuSkuRelationDO.setCreateUser(productRequest.getOperatorUser());
            cspuSkuRelationDO.setUpdateTime(new Date());
            cspuSkuRelationDO.setUpdateUser(productRequest.getOperatorUser());

            cspuSkuRelationDOList.add(cspuSkuRelationDO);
        }
        fullProductData.setCspuSkuRelationDOList(cspuSkuRelationDOList);
        return this;
    }

    public ProductBuilder buildAttributeExtend() {
        AttributeExtendDO attributeExtendDO = new AttributeExtendDO();
        ProductRequest.ItemBaseRequest itemBaseRequest = productRequest.getItemBaseRequest();
        ProductRequest.ItemPopularizeRequest itemPopularizeRequest = productRequest.getItemPopularizeRequest();
        attributeExtendDO.setParticipateId(itemBaseRequest.getItemId());
        attributeExtendDO.setParticipateType(ProductTypeEnum.ITEM.getCode());
        attributeExtendDO.setAttributeContent(JSON.toJSONString(itemPopularizeRequest));
        attributeExtendDO.setVersionId(Objects.isNull(productRequest.getItemBaseRequest().getItemId())? 0:null);
        attributeExtendDO.setDelFlag(DelFlagEnum.EFFECTIVE.getCode());

        attributeExtendDO.setCreateTime(new Date());
        attributeExtendDO.setCreateUser(productRequest.getOperatorUser());
        attributeExtendDO.setUpdateTime(new Date());
        attributeExtendDO.setUpdateUser(productRequest.getOperatorUser());

        fullProductData.setAttributeExtendDO(attributeExtendDO);
        return this;
    }

    public ProductBuilder buildQualityControl() {
        List<QualityControlDO> qualityControlDOList = new ArrayList<>(16);
        ProductRequest.ItemBaseRequest itemBaseRequest = productRequest.getItemBaseRequest();
        ProductRequest.ItemPopularizeRequest itemPopularizeRequest = productRequest.getItemPopularizeRequest();
        List<ProductRequest.QualityControlRequest> qualityControlRequestList = itemPopularizeRequest.getQualityControlRequestList();
        for (ProductRequest.QualityControlRequest qualityControlRequest : qualityControlRequestList) {
            QualityControlDO qualityControlDO = new QualityControlDO();
            qualityControlDO.setItemId(itemBaseRequest.getItemId());
            qualityControlDO.setSkuId(qualityControlRequest.getSkuId());
            qualityControlDO.setQcName(qualityControlRequest.getQcName());
            qualityControlDO.setQcImgUrl(qualityControlRequest.getQcImgUrl());
            qualityControlDO.setQcSort(qualityControlRequest.getQcSort());
            qualityControlDO.setDelFlag(DelFlagEnum.EFFECTIVE.getCode());

            qualityControlDO.setCreateTime(new Date());
            qualityControlDO.setCreateUser(productRequest.getOperatorUser());
            qualityControlDO.setUpdateTime(new Date());
            qualityControlDO.setUpdateUser(productRequest.getOperatorUser());

            qualityControlDOList.add(qualityControlDO);
        }
        fullProductData.setQualityControlDOList(qualityControlDOList);
        return this;
    }

    public FullProductData build() {
        return this.fullProductData;
    }
}
